class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows == 1) return s;
        int d = 2 * numRows - 2, n = s.size();
        string ret;
        for (int i = 0; i < n; i += d)
        {
            ret += s[i];
        }
        for (int k = 1; k < numRows - 1; k++)
        {
            for (int i = k, j = d - k; i < n || j < n; i += d, j += d)
            {
                if (i < n) ret += s[i];
                if (j < n) ret += s[j];
            }
        }
        for (int i = numRows - 1; i < n; i += d)
        {
            ret += s[i];
        }
        return ret;
    }
};
class Solution {
public:
    string countAndSay(int n) {
        string ret = "1";
        for (int i = 1; i < n; i++)
        {
            string tem;
            int len = ret.size();
            for (int left = 0, right = 0; right < len;)
            {
                while (right < len && ret[left] == ret[right]) right++;
                tem += to_string(right - left) + ret[left];
                left = right;
            }
            ret = tem;
        }
        return ret;
    }
};